package com.gz.session;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/loginCheckServlet")
public class LoginCheckServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        if("666666".equals(pwd)){
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            RequestDispatcher mangerServlet = request.getRequestDispatcher("/mangerServlet");
            mangerServlet.forward(request,response);
        }else {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE html>\n" +
                    "<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <title>Title</title>\n" +
                    "  <script type=\"text/javascript\">\n" +
                    "\n" +
                    "  </script>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "<h1>登录失败</h1>\n" +
                    "<a href=\"http://localhost:8080/cookie/login.html\">点击返回重新登陆</a>\n" +
                    "</body>\n" +
                    "</html>");
            out.close();
        }
    }
}
